extends Node2D

var refresh_enemy_time = 0
@onready var background = $ParallaxBackground/background
@onready var timer = $Timer

const enemies = [
	preload("res://scene/object/enemy1.tscn"),
	preload("res://scene/object/enemy2.tscn"),
	preload("res://scene/object/enemy3.tscn"),
	preload("res://scene/object/enemy4.tscn"),
	preload("res://scene/object/enemy5.tscn"),
]

var phase #阶段
var phase_time #当前阶段持续时长
var start_time #当前阶段开始时间
var need_refresh = false

var rock_count # 陨石计数
var enemy_count # 敌人计数
var enemy_limit # 最大敌人数量

func _ready():
	phase = 0


func _process(delta):
	
	# 背景滚动
	background.motion_offset.y+= 150 * delta
	
	# 游戏结束
	if $Player==null || $Player.state!=global.STATE_ACTIVE:
		global.game_over()
		return
	
	if phase == 0:
		phase = 1
	
	if phase>=1 && timer.is_stopped():
		match phase:
			1:
				print("阶段1")
				global.show_trans(1)
				timer.wait_time = 10
				timer.start()
				
			2:
				print("阶段2")
				global.show_trans(2)
				timer.wait_time = 10
				timer.start()
			3:
				pass
	#print(refresh_enemy_time, delta)
	#if get_tree().get_nodes_in_group("enemy").size()==0:
		#refresh_enemy_time+=delta
		#if refresh_enemy_time >=2:
			#refresh_enemy_time = 0
			#randomize()
			#var index = randi_range(0, enemies.size()-1)
			#print(index)
			#var e = enemies[index].instantiate()
			#var x  =randi_range(90, DisplayServer.window_get_size().x-90)
			#e.position = Vector2(x, -40)
			#add_child(e)	


func wait(time):
	await get_tree().create_timer(time).timeout
	
	#need_refresh = false
	#start_time = Time.get_unix_time_from_system()
	#timer.wait_time  = time
	#timer.start()
	
func get_time_duration():
	return Time.get_unix_time_from_system() - start_time	
	


func _on_timer_timeout():
	timer.stop()
	phase+=1
	need_refresh = true
